package com.hc.common.service.impl;

import com.hc.common.service.RedisService;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

@Component
public final class RedisServiceImpl<E> implements RedisService<E> {

    @Resource
    private RedisTemplate<String, E> redisTemplate;


    // =============================common============================

    @Override
    public Set<String> keys(String pattern) {
        try {
            Set<String> keys = redisTemplate.keys(pattern);
            return keys;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }


    @Override
    public boolean expire(String key, Long time) {
        try {
            if (time > 0) {
                redisTemplate.expire(key, time, TimeUnit.SECONDS);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public long getExpire(String key) {
        return redisTemplate.getExpire(key, TimeUnit.SECONDS);
    }

    @Override
    public boolean exists(String key) {
        try {
            return redisTemplate.hasKey(key);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public void del(String... key) {
        if (key != null && key.length > 0) {
            if (key.length == 1) {
                redisTemplate.delete(key[0]);
            } else {
                redisTemplate.delete(Arrays.stream(key).collect(Collectors.toList()));
            }
        }
    }

    // ============================String=============================

    @Override
    public boolean set(String key, E value) {
        try {
            redisTemplate.opsForValue().set(key, value, WEEK_SECONDS, TimeUnit.SECONDS);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }


    @Override
    public boolean set(String key, E value, Long expireTime) {
        try {
            if (expireTime > 0) {
                redisTemplate.opsForValue().set(key, value, expireTime, TimeUnit.SECONDS);
            }
            if (expireTime == -1) {
                //永不过期
                redisTemplate.opsForValue().set(key, value);
            } else {
                set(key, value);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean multiSet(Map<String, E> map) {
        try {
            redisTemplate.opsForValue().multiSet(map);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }


    @Override
    public E get(String key) {
        return key == null ? null : redisTemplate.opsForValue().get(key);
    }


    @Override
    public List<E> multiGet(Collection<String> keys) {
        return redisTemplate.opsForValue().multiGet(keys);
    }


    @Override
    public long incr(String key, Long delta) {
        if (delta < 0) {
            throw new RuntimeException("递增因子必须大于");
        }
        return redisTemplate.opsForValue().increment(key, delta);
    }

    @Override
    public long decr(String key, Long delta) {
        if (delta < 0) {
            throw new RuntimeException("递减因子必须大于");
        }
        return redisTemplate.opsForValue().increment(key, -delta);
    }

    // ================================Map/hash=================================
    //注：Redis的hash的entryKey的类型必须为String类型

    @Override
    public boolean hset(String key, String entryKey, E entryValue) {
        try {
            redisTemplate.opsForHash().put(key, entryKey, entryValue);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean hset(String key, String entryKey, E entryValue, Long time) {
        try {
            redisTemplate.opsForHash().put(key, entryKey, entryValue);
            if (time > 0) {
                expire(key, time);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean hmset(String key, Map<String, E> map) {
        try {
            redisTemplate.opsForHash().putAll(key, map);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean hmset(String key, Map<String, E> map, Long time) {
        try {
            redisTemplate.opsForHash().putAll(key, map);
            if (time > 0) {
                expire(key, time);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public E hget(String key, String item) {
        return (E) redisTemplate.opsForHash().get(key, item);
    }

    @Override
    public <V> Map<String, V> hmget(String key) {
        HashOperations<String, String, V> hashOperations = redisTemplate.opsForHash();
        return hashOperations.entries(key);
    }

    @Override
    public void hdel(String key, String... entryKey) {
        redisTemplate.opsForHash().delete(key, entryKey);
    }

    @Override
    public boolean hHasKey(String key, String entryKey) {
        return redisTemplate.opsForHash().hasKey(key, entryKey);
    }

    @Override
    public double hincr(String key, String entryKey, Double by) {
        return redisTemplate.opsForHash().increment(key, entryKey, by);
    }

    @Override
    public double hdecr(String key, String entryKey, Double by) {
        return redisTemplate.opsForHash().increment(key, entryKey, -by);
    }

    // ============================set=============================

    @Override
    public long sSet(String key, E... values) {
        try {
            return redisTemplate.opsForSet().add(key, values);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public long sSet(String key, Long time, E... values) {
        try {
            long count = redisTemplate.opsForSet().add(key, values);
            if (time > 0) {
                expire(key, time);
            }
            return count;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public Set<E> sGet(String key) {
        try {
            return redisTemplate.opsForSet().members(key);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public boolean sHasKey(String key, E value) {
        try {
            return redisTemplate.opsForSet().isMember(key, value);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public long sGetSetSize(String key) {
        try {
            return redisTemplate.opsForSet().size(key);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public long setRemove(String key, E... values) {
        try {
            Long count = redisTemplate.opsForSet().remove(key, values);
            return count;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }


    // ===============================zset=================================
    @Override
    public boolean zsetAdd(String key, E value, Double score) {
        try {
            redisTemplate.opsForZSet().addIfAbsent(key, value, score);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public long zsetSize(String key) {
        try {
            Long size = redisTemplate.opsForZSet().size(key);
            return size;
        } catch (Exception e) {
            e.printStackTrace();
            return -1;
        }
    }

    @Override
    public boolean zsetRemove(String key, E value) {
        try {
            redisTemplate.opsForZSet().remove(key, value);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean zsetRemoveRange(String key, Long start, Long end) {
        try {
            redisTemplate.opsForZSet().removeRange(key, start, end);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public Set<E> zsetGetRange(String key, Long start, Long end) {
        try {
            Set<E> res = redisTemplate.opsForZSet().range(key, start, end);
            return res;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public void zsetPopMin(String key) {
        try {
            redisTemplate.opsForZSet().popMin(key);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // ===============================list=================================


    @Override
    public boolean lSet(String key, E value) {
        try {
            redisTemplate.opsForList().rightPush(key, value);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean lSet(String key, E value, Long time) {
        try {
            redisTemplate.opsForList().rightPush(key, value);
            if (time > 0) {
                expire(key, time);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean lSets(String key, List<E> value) {
        try {
            redisTemplate.opsForList().rightPushAll(key, value);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }


    @Override
    public boolean lSets(String key, List<E> value, Long time) {
        try {
            redisTemplate.opsForList().rightPushAll(key, value);
            if (time > 0) {
                expire(key, time);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }


    @Override
    public List<E> lGet(String key, Long start, Long end) {
        try {
            return redisTemplate.opsForList().range(key, start, end);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }


    @Override
    public long lGetListSize(String key) {
        try {
            return redisTemplate.opsForList().size(key);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }


    @Override
    public Object lGetIndex(String key, Long index) {
        try {
            return redisTemplate.opsForList().index(key, index);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }


    @Override
    public boolean lUpdateIndex(String key, Long index, E value) {
        try {
            redisTemplate.opsForList().set(key, index, value);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }


    @Override
    public long lRemove(String key, Long count, E value) {
        try {
            Long remove = redisTemplate.opsForList().remove(key, count, value);
            return remove;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }
}
